<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>js</title>
<!-- 存储服务 -->

<script src="https://cdn.jsdelivr.net/npm/leancloud-storage@4.5.3/dist/av-min.js"></script>
<!-- 即时通讯服务 -->
<script src="https://cdn.jsdelivr.net/npm/leancloud-realtime@5.0.0-rc.2/dist/im-browser.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/leancloud-realtime-plugin-typed-messages@3.1.0/dist/typed-messages.min.js"></script> -->
  </head>
  <body>
	<script language=javascript>
		var realtime;
		var mClient;
		var mConversation;
		var mUser;

		function init(id, key, server, user){
			AV.init({
				appId: id, 
				appKey: key,
				serverURLs: server
			});

			var Realtime = AV.Realtime;
			realtime = new Realtime({
				appId: id, 
				appKey: key,
				server: server
				//plugins: [AV.TypedMessagesPlugin], // 注册富媒体消息插件
			});
			
			if(mClient){
				mClient.close();
			}
			realtime.createIMClient(user).then(function(client) {
				mUser = user;
				mClient = client;
				var data ={};
				data.event = "Connected";
				log(data);
				
				LeanMessage.connected();
				
				client.on(AV.Event.MESSAGE, function(message, conversation) {
					var data ={};
					data.event = "gotMessage";
					data.from = message.from;
					data.text = message.text;
					log(data);
					LeanMessage.gotMessage(message.id, message.timestamp, message.from, message.text);
					
				});
				
			}).catch(function(error){
				var data ={};
				data.event = "gotError";
				data.error = error.message;
				log(data);
				LeanMessage.gotError(error.message);
			});
			
		}


		function sendMessage(receiver, message) {
			mClient.createConversation({
					members: [receiver],
					unique: true
			}).then(function(conversation) {
				var textMessage = new AV.TextMessage(message)
				return conversation.send(textMessage);
			}).then(function(msg) {
				var data ={};
				data.event = "messageSent";
				//data.message = msg;
				log(data);
				LeanMessage.messageSent();
			}).catch(function(error){
				var data ={};
				data.event = "gotError";
				data.error = error.message;
				log(data);
				LeanMessage.gotError(error.message);
			});
		}

		
		function getHistory(otherUser,count){
			
			var options = {
						limit: count, 
					};
			doGetHistories(otherUser,options);
		}
		
		function getHistoryFrom(otherUser, fromId, fromTime){
			
			var options = {
						startTime: new Date(fromTime).getTime(),
						startMessageId: fromId,
						startClosed: false,
						direction: AV.MessageQueryDirection.OLD_TO_NEW
					};
			doGetHistories(otherUser,options);
		}
		
		
		function doGetHistories(otherUser,options){
			if(mClient){
				var query = mClient.getQuery();
				query.containsAll('m',[mUser,otherUser]);
				query.find().then(function(conversations) {
					conversation=conversations[0];
					conversation.queryMessages(options).then(function(messages) {
						var data ={};
						data.event = "gotHistories";
						var histories =[];
						for(var i=0;i<messages.length;i++){
							var history = {};
							history.from=messages[i].from;
							history.text=messages[i].text;
							history.id=messages[i].id;
							history.timestamp=messages[i].timestamp;							
							histories.push(history);
						}
						data.histories=histories;
						log(data);
						LeanMessage.gotHistories(JSON.stringify(histories),messages.length);
					}).catch(function(error){
						var data ={};
						data.event = "gotError";
						data.error = error.message;
						log(data);
						LeanMessage.gotError(error.message);
					});
				}).catch(function(error){
					var data ={};
					data.event = "gotError";
					data.error = error.message;
					log(data);
					LeanMessage.gotError(error.message);
				});
			}
		}
		
		
		
		function log(d){
			if(window.AppInventor){
				window.AppInventor.setWebViewString(JSON.stringify(d));
			}
			console.log(JSON.stringify(d));
		}
		
		


	</script>
  </body>
</html>
